import { defineStore } from "pinia";
import type { Category } from "./interface";
import {
  getCategoty1Api,
  getCategory2Api,
  getCategory3Api,
} from "@/api/product/categotySelector";
export const userCategoryStore = defineStore("category", {
  state: (): Category => {
    return {
      categoty1Id: undefined,
      categoty2Id: undefined,
      categoty3Id: undefined,
      categoty1List: [],
      categoty2List: [],
      categoty3List: [],
    };
  },
  getters: {},
  actions: {
    // 一级分类
    async category1List() {
      const result = await getCategoty1Api();
      this.categoty1List = result;
    },
    // 二级分类
    async category2List(newVal: number) {
      
      this.categoty2List = await getCategory2Api(newVal);
      this.categoty1Id = newVal;
      this.categoty2Id = undefined; //重新赋值功能就是选上级时，后面数据清空
      this.categoty3Id = undefined;
      this.categoty3List = [];
    },
    // 三级分类
    async category3List(newVal2: number) {
     this.categoty3List = await getCategory3Api(newVal2);
      this.categoty2Id = newVal2;
      //  console.log(category.categoty3List);
     this.categoty3Id = undefined;
    },
  },
});
